package com.yic.module.wms.service.warehouse;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yic.module.system.enums.dict.SystemBooleanEnum;
import com.yic.module.wms.dal.dataobject.warehousearea.WarehouseAreaDO;
import com.yic.module.wms.dal.redis.RedisKeyConstants;
import com.yic.module.wms.service.warehousearea.WarehouseAreaService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yic.module.wms.controller.admin.warehouse.vo.*;
import com.yic.module.wms.dal.dataobject.warehouse.WarehouseDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.wms.convert.warehouse.WarehouseConvert;
import com.yic.module.wms.dal.mysql.warehouse.WarehouseMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.wms.enums.ErrorCodeConstants.*;

/**
 * 仓库 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class WarehouseServiceImpl implements WarehouseService {

    @Resource
    private WarehouseMapper warehouseMapper;
    @Resource
    @Lazy
    private WarehouseAreaService warehouseAreaService;

    @Override
    public Long createWarehouse(WarehouseCreateReqVO createReqVO) {
        validateCreateOrUpdate(null, createReqVO.getCode());
        // 插入
        WarehouseDO warehouse = WarehouseConvert.INSTANCE.convert(createReqVO);
        warehouseMapper.insert(warehouse);
        // 返回
        return warehouse.getId();
    }

    @Override
    @CacheEvict(cacheNames = RedisKeyConstants.WAREHOUSE, key = "#updateReqVO.id")
    public void updateWarehouse(WarehouseUpdateReqVO updateReqVO) {
        // 校验存在
        validateCreateOrUpdate(updateReqVO.getId(), updateReqVO.getCode());
        if (updateReqVO.getOpenArea().equals(SystemBooleanEnum.NO.getSystemBoolean())) {
            validateWarehouseAreaExists(updateReqVO.getId());
        }
        // 更新
        WarehouseDO updateObj = WarehouseConvert.INSTANCE.convert(updateReqVO);
        warehouseMapper.updateById(updateObj);
    }

    @Override
    @CacheEvict(cacheNames = RedisKeyConstants.WAREHOUSE, key = "#id")
    public void deleteWarehouse(Long id) {
        // 校验存在
        WarehouseDO warehouseDO = validateWarehouseExists(id);
        //校验是否存在库区
        validateWarehouseAreaExists(warehouseDO.getId());
        // 删除
        warehouseMapper.deleteById(id);
    }

    private void validateCreateOrUpdate(Long id, String code){
        validateCodeExists(id, code);
    }

    private void validateCodeExists(Long id, String code) {
        WarehouseDO warehouseDO = warehouseMapper.selectByCode(code);
        if (ObjectUtil.isEmpty(warehouseDO)) {
            return;
        }
        if (id == null) {
            throw exception(WAREHOUSE_CODE_EXISTS);
        }
        if (!id.equals(warehouseDO.getId())) {
            throw exception(WAREHOUSE_CODE_EXISTS);
        }
    }

    private void validateWarehouseAreaExists(Long warehouseId) {
        List<WarehouseAreaDO> warehouseAreaDOS = warehouseAreaService.getWarehouseAreaListByWarehouseId(warehouseId);
        if (CollectionUtil.isNotEmpty(warehouseAreaDOS)) {
            throw exception(WAREHOUSE_NOT_DEL_EDIT);
        }
    }

    private WarehouseDO validateWarehouseExists(Long id) {
        WarehouseDO warehouseDO = warehouseMapper.selectById(id);
        if (warehouseDO == null) {
            throw exception(WAREHOUSE_NOT_EXISTS);
        }
        return warehouseDO;
    }

    @Override
    @Cacheable(cacheNames = RedisKeyConstants.WAREHOUSE, key = "#id")
    public WarehouseDO getWarehouse(Long id) {
        return warehouseMapper.selectById(id);
    }

    @Override
    public List<WarehouseDO> getWarehouseList(Collection<Long> ids) {
        return warehouseMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<WarehouseDO> getWarehousePage(WarehousePageReqVO pageReqVO) {
        return warehouseMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WarehouseDO> getWarehouseList(WarehouseExportReqVO exportReqVO) {
        return warehouseMapper.selectList(exportReqVO);
    }

    @Override
    public List<WarehouseDO> getWarehouseList(Collection<Long> ids, Collection<Integer> statuses) {
        return warehouseMapper.selectList(ids, statuses);
    }

    @Override
    public WarehouseDO getWarehouseByErpId(String erpWarehouseId) {
        return warehouseMapper.selectByErpId(erpWarehouseId);
    }

    @Override
    public List<WarehouseDO> getWarehouseListByErpId(Collection<String> erpWarehouseIds) {
        return warehouseMapper.selectListByErpId(erpWarehouseIds);
    }

}
